import axios from "@/utils/axios";

const action = {
    async getPatrolMessage({ commit, state }, orgName) {
        // 获取当前时间
        let date = new Date();
        let year = "";
        // let year = date.getFullYear();
        //  + '-' + Number(date.getFullYear() + 1);
        // let semester = date.getMonth() < 3 || date.getMonth() > 9 ? 1 : 2;
        let semester = -1;

        if (date.getMonth() < 3) {
            year = date.getFullYear() - 1 + "-" + date.getFullYear();
            semester = 1;
        } else if (date.getMonth() > 9) {
            year = date.getFullYear() + "-" + (date.getFullYear() + 1);
            semester = 1;
        } else {
            year = date.getFullYear() - 1 + "-" + date.getFullYear();
            semester = 2;
        }
        console.log("🚀 ~ file: action.js ~ line 28 ~ getPatrolMessage ~ year", year)
        let Day = date.getDay();
        const timeData = {
            year: year,
            // semester: semester,
        }
        // 获取patrol信息
        let queryString = orgName;
        const url = "/pc/v1/timetables/patrol/" + queryString;
        console.log("🚀 ~ file: action.js ~ line 37 ~ getPatrolMessage ~ url", url)
        try {
            let {data} = await axios.post(url, timeData);
            console.log("🚀 ~ file: action.js ~ line 40 ~ getPatrolMessage ~ data", data)
            // 选择树
            let treeData = [];
            data.data.forEach(item1 => {
                if (!treeData || treeData.length === 0) {
                    treeData.push({ showName: item1.course_id.subOrg_name, childMenu: [] })
                }
                treeData.forEach((item2, index, array) => {
                    if (item1.course_id.subOrg_name == item2.showName) {
                        item2.childMenu.push({ showName: item1.course_id.major_name })
                    }
                    else {
                        array.push({ showName: item1.course_id.subOrg_name, childMenu: [] })
                    }
                })
            })
            commit("updatePatrolTree", treeData)
            // data = data.data;
            // 模糊查询
            if (state.menuSelect.subOrg != "陕西师范大学") {
                data.data = data.data.filter(item => {
                    return item.course_id.subOrg_name == state.menuSelect.subOrg;
                })
            }
            // 数据处理 排巡课前的课表
            let patrolScheduleTable = [];
            data.data.forEach(course => {
                course.curriculum.forEach(item => {
                    for (let index = 1; index <= 10; index++) {
                        if (patrolScheduleTable[index - 1] == undefined) {
                            patrolScheduleTable[index - 1] = [];
                        }
                        if (item.order.indexOf(index + "") > -1) {
                            // 分周定三维数组
                            switch (item.date) {
                                case "Mon":
                                    assignScheduleDate(patrolScheduleTable, 0, index, course, item);
                                    break;
                                case "Tue":
                                    assignScheduleDate(patrolScheduleTable, 1, index, course, item);
                                    break;
                                case "Wed":
                                    assignScheduleDate(patrolScheduleTable, 2, index, course, item);
                                    break;
                                case "Thu":
                                    assignScheduleDate(patrolScheduleTable, 3, index, course, item);
                                    break;
                                case "Fri":
                                    assignScheduleDate(patrolScheduleTable, 4, index, course, item);
                                    break;
                                case "Sat":
                                    assignScheduleDate(patrolScheduleTable, 5, index, course, item);
                                    break;
                                case "Sun":
                                    assignScheduleDate(patrolScheduleTable, 6, index, course, item);
                                    break;

                            }
                        }
                    }
                })
            });
            // 巡课数据
            data = data.data.map(item => {
                return {
                    subOrg: item.course_id.subOrg_name,
                    major: item.course_id.major_name,
                    course: { _id: item.course_id._id, name: item.course_id.name },
                    timeTable_id: item._id,
                    lesson_id: item.lesson_id,
                    teacher: item.teacher_id,
                    class: item.curriculum[0].class_id,
                    room: item.curriculum[0].room_id
                }
            })
            commit("updatePatrolMessage", data);
            commit("updatePatrolSchedule", patrolScheduleTable);
            // 数据处理

        } catch (err) {
            // 如果报错为巡课信息为空，则将巡课信息与巡课树改为空
            // 存在出错信息判断问题
            commit("updatePatrolMessage", [])
            // commit("updatePatrolTree", [])
            console.error(err);
        }
    },
    async getPatrolTree({ commit }, oid) {
        const url = `/pc/v1/organizations/${oid}/tree`;
        try {
            const { data } = await axios.get(url);
            commit("updatePatrolTree", data.tree)
        } catch (err) {
            commit("updatePatrolTree", [])
            console.log(err);
        }
    },
};

function assignScheduleDate(patrolScheduleTable, date, index, course, item) {
    if (patrolScheduleTable[index - 1][date] == undefined) {
        patrolScheduleTable[index - 1][date] = [];
    }
    patrolScheduleTable[index - 1][date].push({
        date: item.date,
        lesson_id: course.lesson_id,
        course: { _id: course.course_id._id, name: course.course_id.name, course_type: course.course_id.course_type },
        teacher: { _id: course.teacher_id.id, name: course.teacher_id.name },
        room: item.room_id
    })
}

export default action;
